package io.framework.modules.hpinfo.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.ExcelUtils;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.hpinfo.dto.VTHpglHpspbhqjfjqDTO;
import io.framework.modules.hpinfo.excel.VTHpglHpspbhqjfjqExcel;
import io.framework.modules.hpinfo.service.VTHpglHpspbhqjfjqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 环境影响评价管理-环评审批保护区及风景区情况
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-19
 */
@RestController
@Api(tags = "环境影响评价管理-环评审批保护区及风景区情况")
public class VTHpglHpspbhqjfjqController {
    @Autowired
    private VTHpglHpspbhqjfjqService vTHpglHpspbhqjfjqService;

    @GetMapping("hpinfo/vthpglhpspbhqjfjq/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    public Result<PageData<VTHpglHpspbhqjfjqDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {

        PageData<VTHpglHpspbhqjfjqDTO> page = vTHpglHpspbhqjfjqService.pages(params);

        return new Result<PageData<VTHpglHpspbhqjfjqDTO>>().ok(page);
    }

    @GetMapping("hpinfo/vthpglhpspbhqjfjq/{id}")
    @ApiOperation("信息")
    public Result<VTHpglHpspbhqjfjqDTO> get(@PathVariable("id") String id) {
        VTHpglHpspbhqjfjqDTO data = vTHpglHpspbhqjfjqService.selectById(id);

        return new Result<VTHpglHpspbhqjfjqDTO>().ok(data);
    }

    @GetMapping("hpinfo/vthpglhpspbhqjfjq/getProjectInfoByProjectId/{id}")
    @ApiOperation("根据建设项目ID查询-环评审批保护区及风景区情况")
    public Result<VTHpglHpspbhqjfjqDTO> getProjectInfoByProjectId(@PathVariable("id") String id) {
        VTHpglHpspbhqjfjqDTO data = vTHpglHpspbhqjfjqService.getProjectInfoByProjectId(id);

        return new Result<VTHpglHpspbhqjfjqDTO>().ok(data);
    }

    @GetMapping("hpinfo/vthpglhpspbhqjfjq/projectInfo/bindProjectInfo")
    @ApiOperation("绑定环评建设项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkId", value = "环评审批保护区及风景区情况 ID", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "projectId", value = "绑定环评建设项目ID", paramType = "query", required = true, dataType = "Long"),
    })
    public Result bind(@ApiIgnore @RequestParam Map<String, Object> params) {

        int flag = vTHpglHpspbhqjfjqService.bindProjectInfo(params);

        if (flag < 1) {
            return new Result().error(ErrorCode.UPDATE_INFO_ERROR);
        }
        return new Result();
    }

    @PostMapping("hpinfo/vthpglhpspbhqjfjq/save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(VTHpglHpspbhqjfjqDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setPkId(IdWorker.get32UUID());
        dto.setCreatetime(new Date());
        vTHpglHpspbhqjfjqService.save(dto);

        return new Result();
    }

    @PutMapping("hpinfo/vthpglhpspbhqjfjq/update")
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(VTHpglHpspbhqjfjqDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dto.setUpdatedate(new Date());
        vTHpglHpspbhqjfjqService.update(dto);

        return new Result();
    }

    @DeleteMapping("hpinfo/vthpglhpspbhqjfjq/delete/{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@PathVariable("id") String id) {
        //效验数据
        AssertUtils.isBlank("id", ErrorCode.IDENTIFIER_NOT_NULL);

        vTHpglHpspbhqjfjqService.deleteById(id);

        return new Result();
    }

}